/*
*********************************************************************************************************
*                                       uC/OS-III Application Code
*
* Filename      : button.c
* Version       : V1.00
* Author        : Jimmy
* Date          : 2018-10-22
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*                                             INCLUDE FILES
*********************************************************************************************************
*/

#include <includes.h>

/*
*********************************************************************************************************
*                                           LOCAL VARIABLES
*********************************************************************************************************
*/
  KEY_TAB         key;
  
  KEY_PRESS_STATE key_state;

/*
*********************************************************************************************************
*                                         FUNCTION PROTOTYPES
*********************************************************************************************************
*/
void Button_Scan(KEY_TAB *key);
void Button_Read_State(KEY_TAB *key);
void Button_1_Handle(KEY_TAB *key);
void Button_2_Handle(KEY_TAB *key);
void Button_3_Handle(KEY_TAB *key);
void Button_Handle(KEY_TAB *key);
void Button_Adc_Handle();

/*
*********************************************************************************************************
*                                             Button_Init()
*
* Description : 
*
* Arguments   : none
*
* Returns     : none
*
* Author      : Jimmy
*
* Date        : 2018-10-22
*********************************************************************************************************
*/
void Button_Init(KEY_TAB  *key)
{
  key->key_press[BUTTON_1_INDEX] = FLAG_NO;
  key->key_press[BUTTON_2_INDEX] = FLAG_NO;
  key->key_press[BUTTON_3_INDEX] = FLAG_NO;
}


/*
*********************************************************************************************************
*                                             Serve_Button()
*
* Description : 
*
* Arguments   : none
*
* Returns     : none
*
* Author      : Jimmy
*
* Date        : 2018-10-22
*********************************************************************************************************
*/
void Serve_Button(void)
{
  Button_Scan(&key);
  Button_Read_State(&key);
  Button_Handle(&key);

}

/*
*********************************************************************************************************
*                                             Button_Scan()
*
* Description : 
*
* Arguments   : none
*
* Returns     : none
*
* Author      : Jimmy
*
* Date        : 2018-10-22
*********************************************************************************************************
*/
void Button_Scan(KEY_TAB  *key)
{
    key->key_value [BUTTON_1_INDEX] = (KEY_STATE)gi_key_0;
    key->key_value [BUTTON_2_INDEX] = (KEY_STATE)gi_key_1;
    key->key_value [BUTTON_3_INDEX] = (KEY_STATE)gi_key_up;
}

/*
*********************************************************************************************************
*                                             Button_Read_State()
*
* Description : 
*
* Arguments   : none
*
* Returns     : none
*
* Author      : Jimmy
*
* Date        : 2018-10-22
*********************************************************************************************************
*/

void Button_Read_State(KEY_TAB *key)
{
  static int x = 0;

  for(x=1;x<KEY_NUM-2;x++)
  {
    if((key->key_value[x] == KEY_PRESS)&&(key->key_press_time[x] <= KEY_HOLD_TIME))
    {
      key->key_press_time[x] ++;
      key->key_no_press_time[x]= 0;
    }
    if((key->key_value[x] == KEY_NO_PRESS)&&(key->key_no_press_time[x] <= KEY_HOLD_TIME))
    {
      key->key_press_time[x] = 0;
      key->key_no_press_time[x] ++;

    }
  }

  if((key->key_value[3] == 1)&&(key->key_press_time[3] <= KEY_HOLD_TIME))
  {
    key->key_press_time[3] ++;
    key->key_no_press_time[3] = 0;
  }
  if((key->key_value[3] == 0)&&(key->key_no_press_time[3] <= KEY_HOLD_TIME))
  {
    key->key_press_time[3] = 0;
    key->key_no_press_time[3] ++;

  }
}

/*
*********************************************************************************************************
*                                             Button_Handle()
*
* Description : 
*
* Arguments   : none
*
* Returns     : none
*
* Author      : Jimmy
*
* Date        : 2018-10-22
*********************************************************************************************************
*/
void Button_Handle(KEY_TAB *key)
{
  Button_1_Handle (key);
  Button_2_Handle (key);
  Button_3_Handle (key);
}

/*
*********************************************************************************************************
*                                             Button_1_Handle()
*
* Description : 
*
* Arguments   : none
*
* Returns     : none
*
* Author      : Jimmy
*
* Date        : 2018-10-22
*********************************************************************************************************
*/
void Button_1_Handle(KEY_TAB *key)
{
    if((key->key_press_time  [BUTTON_1_INDEX] >  KEY_SHORT_PRESS)
     &&(key->key_press_time  [BUTTON_1_INDEX] <  KEY_LONG_PRESS )
     &&(key->key_press       [BUTTON_1_INDEX] == FLAG_NO)
     &&(key->key_execute_flag[BUTTON_1_INDEX] == FLAG_NO))
    {
      if(key->key_no_press_time[BUTTON_1_INDEX] > 0)
      {
        key->key_press        [BUTTON_1_INDEX] = FLAG_YES;
  //      key->key_press_time   [BUTTON_1_INDEX] = 0;
        key->key_no_press_time[BUTTON_1_INDEX] = 0;
      }
      
    }
  
    if((key->key_press_time  [BUTTON_1_INDEX] >  KEY_LONG_PRESS)
     &&(key->key_long_press  [BUTTON_1_INDEX] == FLAG_NO)
     &&(key->key_execute_flag[BUTTON_1_INDEX] == FLAG_NO))
    {
      key->key_long_press   [BUTTON_1_INDEX] = FLAG_YES;
  //    key->key_press_time   [BUTTON_1_INDEX] = 0;
      key->key_no_press_time[BUTTON_1_INDEX] = 0;
    }
    
    if((key->key_execute_flag [BUTTON_1_INDEX] == FLAG_YES)
     &&(key->key_no_press_time[BUTTON_1_INDEX] > KEY_NO_PRESS_CNT))
    {
      key->key_execute_flag [BUTTON_1_INDEX] = FLAG_NO;
      key->key_press        [BUTTON_1_INDEX] = FLAG_NO;
      key->key_long_press   [BUTTON_1_INDEX] = FLAG_NO;
      key->key_press_time   [BUTTON_1_INDEX] = 0;
    }

}

/*
*********************************************************************************************************
*                                             Button_2_Handle()
*
* Description : 
*
* Arguments   : none
*
* Returns     : none
*
* Author      : Jimmy
*
* Date        : 2018-10-22
*********************************************************************************************************
*/
void Button_2_Handle(KEY_TAB *key)
{
  if((key->key_press_time  [BUTTON_2_INDEX] >  KEY_SHORT_PRESS)
   &&(key->key_press_time  [BUTTON_2_INDEX] <  KEY_LONG_PRESS )
   &&(key->key_press       [BUTTON_2_INDEX] == FLAG_NO)
   &&(key->key_execute_flag[BUTTON_2_INDEX] == FLAG_NO))
  {
    if(key->key_no_press_time[BUTTON_2_INDEX] > 0)
    {
      key->key_press        [BUTTON_2_INDEX] = FLAG_YES;
      key->key_press_time   [BUTTON_2_INDEX] = 0;
      key->key_no_press_time[BUTTON_2_INDEX] = 0;
    }
    
  }

  if((key->key_press_time  [BUTTON_2_INDEX] >  KEY_LONG_PRESS)
   &&(key->key_long_press  [BUTTON_2_INDEX] == FLAG_NO)
   &&(key->key_execute_flag[BUTTON_2_INDEX] == FLAG_NO))
  {
    key->key_long_press   [BUTTON_2_INDEX] = FLAG_YES;
    key->key_press_time   [BUTTON_2_INDEX] = 0;
    key->key_no_press_time[BUTTON_2_INDEX] = 0;
  }
  
  if((key->key_execute_flag [BUTTON_2_INDEX] == FLAG_YES)
   ||(key->key_no_press_time[BUTTON_2_INDEX] > KEY_NO_PRESS_CNT))
  {
    key->key_execute_flag [BUTTON_2_INDEX] = FLAG_NO;
    key->key_press        [BUTTON_2_INDEX] = FLAG_NO;
    key->key_long_press   [BUTTON_2_INDEX] = FLAG_NO;
    key->key_press_time   [BUTTON_2_INDEX] = 0;
  }

}


/*
*********************************************************************************************************
*                                             Button_3_Handle()
*
* Description : 
*
* Arguments   : none
*
* Returns     : none
*
* Author      : Jimmy
*
* Date        : 2018-10-22
*********************************************************************************************************
*/
void Button_3_Handle(KEY_TAB *key)
{
  if((key->key_press_time  [BUTTON_3_INDEX] >  KEY_SHORT_PRESS)
   &&(key->key_press_time  [BUTTON_3_INDEX] <  KEY_LONG_PRESS )
   &&(key->key_press       [BUTTON_3_INDEX] == FLAG_NO)
   &&(key->key_execute_flag[BUTTON_3_INDEX] == FLAG_NO))
  {
    if(key->key_no_press_time[BUTTON_3_INDEX] > 0)
    {
      key->key_press        [BUTTON_3_INDEX] = FLAG_YES;
      key->key_press_time   [BUTTON_3_INDEX] = 0;
      key->key_no_press_time[BUTTON_3_INDEX] = 0;
    }
    
  }

  if((key->key_press_time  [BUTTON_3_INDEX] >  KEY_LONG_PRESS)
   &&(key->key_long_press  [BUTTON_3_INDEX] == FLAG_NO)
   &&(key->key_execute_flag[BUTTON_3_INDEX] == FLAG_NO))
  {
    key->key_long_press   [BUTTON_3_INDEX] = FLAG_YES;
    key->key_press_time   [BUTTON_3_INDEX] = 0;
    key->key_no_press_time[BUTTON_3_INDEX] = 0;
  }
  
  if((key->key_execute_flag [BUTTON_3_INDEX] == FLAG_YES)
   ||(key->key_no_press_time[BUTTON_3_INDEX] > KEY_NO_PRESS_CNT))
  {
    key->key_execute_flag [BUTTON_3_INDEX] = FLAG_NO;
    key->key_press        [BUTTON_3_INDEX] = FLAG_NO;
    key->key_long_press   [BUTTON_3_INDEX] = FLAG_NO;
    key->key_press_time   [BUTTON_3_INDEX] = 0;
  }

}

